/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.rest;

import cn.hutool.core.convert.Convert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.StorehouseOrder;
import me.zhengjie.modules.biz.domain.StorehouseOrderDetail;
import me.zhengjie.modules.biz.service.StorehouseOrderDetailService;
import me.zhengjie.modules.biz.service.dto.StorehouseOrderDetailQueryCriteria;
import me.zhengjie.modules.utils.BaseResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Admin
 * @website https://eladmin.vip
 * @date 2023-03-13
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "storehouse-order-detail管理")
@RequestMapping("/api/storehouseOrderDetail")
public class StorehouseOrderDetailController {

    private final StorehouseOrderDetailService storehouseOrderDetailService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('storehouseOrderDetail:list')")
    public void exportStorehouseOrderDetail(HttpServletResponse response, StorehouseOrderDetailQueryCriteria criteria) throws IOException {
        storehouseOrderDetailService.download(storehouseOrderDetailService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询storehouse-order-detail")
    @ApiOperation("查询storehouse-order-detail")
//    @PreAuthorize("@el.check('storehouseOrderDetail:list')")
    public ResponseEntity<Object> queryStorehouseOrderDetail(StorehouseOrderDetailQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(storehouseOrderDetailService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增storehouse-order-detail")
    @ApiOperation("新增storehouse-order-detail")
//    @PreAuthorize("@el.check('storehouseOrderDetail:add')")
    public ResponseEntity<Object> createStorehouseOrderDetail(@Validated @RequestBody StorehouseOrderDetail resources) {
        return new ResponseEntity<>(storehouseOrderDetailService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改storehouse-order-detail")
    @ApiOperation("修改storehouse-order-detail")
//    @PreAuthorize("@el.check('storehouseOrderDetail:edit')")
    public ResponseEntity<Object> updateStorehouseOrderDetail(@Validated @RequestBody StorehouseOrderDetail resources) {
        storehouseOrderDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除storehouse-order-detail")
    @ApiOperation("删除storehouse-order-detail")
//    @PreAuthorize("@el.check('storehouseOrderDetail:del')")
    public ResponseEntity<Object> deleteStorehouseOrderDetail(@RequestBody Integer[] ids) {
        storehouseOrderDetailService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/list")
    public ResponseEntity<Object> list(Integer id) {
        return new ResponseEntity<>(storehouseOrderDetailService.detailList(id), HttpStatus.OK);
    }

    @PostMapping("return")
    public ResponseEntity<Object> giveBack(@RequestBody Map<String, Object> data) throws Exception {
        return new ResponseEntity<>(storehouseOrderDetailService.giveBack(Convert.toStr(data.get("data"))), HttpStatus.OK);
    }

    @PostMapping("return-v2")
    public ResponseEntity<Object> giveBackV2(@RequestBody Map<String, Object> data) throws Exception {
        try {
            int res = storehouseOrderDetailService.giveBack(Convert.toStr(data.get("data")));
            return new ResponseEntity<>(new BaseResult(0, "归还成功", res), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new BaseResult(-1, e.getMessage()), HttpStatus.OK);
        }
    }

    //web 归还列表
    @GetMapping("returnList")
    public ResponseEntity<Object> returnList(HttpServletRequest request) {
        return new ResponseEntity<>(storehouseOrderDetailService.returnList(request), HttpStatus.OK);
    }

    //mobile 已归还列表
    @GetMapping("returnedList")
    @Log("mobile 已归还列表")
    public ResponseEntity<Object> returnedList(HttpServletRequest request) {
        return new ResponseEntity<>(storehouseOrderDetailService.returnedList(request), HttpStatus.OK);
    }

    //mobile 待归还列表
    @GetMapping("noReturnedList")
    @Log("mobile 待归还列表")
    public ResponseEntity<Object> noReturnedList(HttpServletRequest request) {
        return new ResponseEntity<>(storehouseOrderDetailService.noReturnedList(request), HttpStatus.OK);
    }

}
